package utility;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

/**
 * The class contains several useful static methods used by different classes
 * @author Neb
 *
 */
public class GraphicUtilsFactory
{
	/**
	 * Returns a titled border
	 * @param title the title o the border to return
	 * @return a titled border
	 */
	public static Border createTitledBorder(String title)
	{
		Border border = BorderFactory.createLineBorder(Color.black, 1);
		TitledBorder borderTitle = BorderFactory.createTitledBorder(border, title);
		borderTitle.setTitleColor(Color.black);
		borderTitle.setTitleFont( new Font("Verdana", Font.BOLD, 12));
		
		return borderTitle;
	}
	
	/**
	 * Sets customized parameters to the text field given
	 * @param field the text field to which set the parameters
	 * @param borderTitle the title of the border to add to the text field
	 */
	public static void setTitleAndBorderToComponent(JComponent field, String borderTitle)
	{
		field.setOpaque(false);
		field.setBorder(GraphicUtilsFactory.createTitledBorder(borderTitle));
		field.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
		field.setForeground(Color.BLACK);
	}
	
	public static void packTableColoumns(JTable table, int margin)
	{ 
		for (int c = 0; c < table.getColumnCount(); c++)
			GraphicUtilsFactory.packColumn(table, c, 2); 
	}
	
	protected static void packColumn(JTable table, int vColIndex, int margin)
	{ 
		DefaultTableColumnModel colModel = (DefaultTableColumnModel)table.getColumnModel(); 
		TableColumn col = colModel.getColumn(vColIndex);
		int width = 0; // Get width of column header 
		TableCellRenderer renderer = col.getHeaderRenderer();
		if (renderer == null) 
			renderer = table.getTableHeader().getDefaultRenderer(); 

		Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
		width = comp.getPreferredSize().width; // Get maximum width of column data 
		for (int r = 0; r < table.getRowCount(); r++) 
		{ 
			renderer = table.getCellRenderer(r, vColIndex); 
			comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false, r, vColIndex); 
			width = Math.max(width, comp.getPreferredSize().width);
		} // Add margin 
		width += 2*margin; // Set the width 
		col.setPreferredWidth(width);
	}
	
	public static Style getUserIdMessageStyle(StyledDocument styledDocument)
	{
		Style def = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE );
		
	    Style style = styledDocument.addStyle( "userId", def );
	    StyleConstants.setSuperscript( style, true );
	    StyleConstants.setFontFamily(style, "Comic Sans MS");
	    StyleConstants.setFontSize( style, 15 );
	    StyleConstants.setForeground(style, Color.black);
	    StyleConstants.setBold( style, false );
	    
	    return style;
	    
	}
	
	public static Style getChatMessageStyle(StyledDocument styledDocument)
	{
		Style def = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE );
		
	    Style style = styledDocument.addStyle( "chat", def );
	    StyleConstants.setSuperscript( style, true );
	    StyleConstants.setFontFamily(style, "Comic Sans MS");
	    StyleConstants.setFontSize( style, 15 );
	    StyleConstants.setForeground(style, Color.black);
	    StyleConstants.setBold( style, false );
	    
	    return style;
	}
	
}